package juc.thread.reentrantLock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Test5 {
    static boolean money = false;
    static boolean yan = false;
    private static final ReentrantLock lock=new ReentrantLock();
    private static Condition moneyWaitingSet=lock.newCondition();
    private static Condition yanWaitingSet=lock.newCondition();
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
               lock.lock();
            try {
                while (!money) {
                    try {
                        System.out.println("没加班费不干...");
                        moneyWaitingSet.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("00后开始干活...");
            } finally {
                lock.unlock();
            }
        }, "00后").start();
        new Thread(() -> {
            lock.lock();
            try {
                while (!yan) {
                    try {
                        System.out.println("没烟不干...");
                        yanWaitingSet.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("90后开始干活...");
            }finally {
                lock.unlock();
            }
        }, "90后").start();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                lock.lock();
                try {
                    System.out.println("普?开始干活");
                }finally {
                    lock.unlock();
                }
            }, "普" + i).start();
        }
        Thread.sleep(1000);
        lock.lock();
       try {
            System.out.println("给加班费...");
            money = true;
            moneyWaitingSet.signalAll();
        }finally {
           lock.unlock();
       }
        Thread.sleep(1000);
        lock.lock();
        try {
            System.out.println("给烟...");
            yan = true;
            yanWaitingSet.signalAll();
        }finally {
            lock.unlock();
        }
    }
}
